<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class UnitAvailability {
    
    private $reservations;
    private $number_of_units;
    
    public function is_unit_free($start, $end, $unit_number) {
        if($unit_number > $this->number_of_units){
            return 'There is no such a unit';
        }
        if (!isset($this->reservations) || empty($this->reservations)) {
            return 'Reservations not set';
        }
        $strt = $start;
        while ($strt <= $end) {
            for ($index = 0; $index < count($this->reservations); $index++) {
                $s = $this->reservations[$index]['start'];
                $e = $this->reservations[$index]['end'];
                $number_of_unit = $this->reservations[$index]['unit_number'];
                while ($s<=$e){
                 if($s==$strt && $number_of_unit == $unit_number){
                     return 'Unit is reserved for that period';
                 } 
                 $s = strtotime('+1 day', $s);
                }
            }
            $strt = strtotime('+1 day', $strt);
        }
        return 'Unit is free for reservation';
    }
    
    public function getReservations() {
        return $this->reservations;
    }

    public function setReservations($reservations) {
        $this->reservations = $reservations;
    }
    
    public function getNumber_of_units() {
        return $this->number_of_units;
    }

    public function setNumber_of_units($number_of_units) {
        $this->number_of_units = $number_of_units;
    }
}